package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;
    @Override
    public void save(ExtCproduct extCproduct) {
        //小计金额
        Double amount =extCproduct.getCnumber()*extCproduct.getPrice();
        extCproduct.setAmount(amount);
        //查询合同
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setExtNum(contract.getExtNum()+1);
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        //更新
        contractDao.updateByPrimaryKeySelective(contract);
        extCproductDao.insertSelective(extCproduct);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        //更新总金额  :   总金额-旧的小计金额+新的小计金额
        //新的小计
        Double newAmount=extCproduct.getPrice()*extCproduct.getCnumber();
        extCproduct.setAmount(newAmount);
        //查询旧的小计金额
        ExtCproduct extCproduct_old = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        Double oleAmount = extCproduct_old.getAmount();
        //查询总金额
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        Double totalAmount = contract.getTotalAmount();
        contract.setTotalAmount(totalAmount-oleAmount+newAmount);
        contractDao.updateByPrimaryKeySelective(contract);
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

    @Override
    public void delete(String id) {
        //1.根据id查询附件
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        //2.获取删除附件的总金额
        Double money = extCproduct.getAmount();
        //3.查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //4.设置购销合同总金额
        contract.setTotalAmount(contract.getTotalAmount() - money);
        //5.设置购销合同的附件数
        contract.setExtNum(contract.getExtNum() - 1);
        //6.更新合同
        contractDao.updateByPrimaryKeySelective(contract);
        //7.删除附件
        extCproductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list,5);
    }
}
